﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    class Service_CIB_Deposit
    {
        protected DA_CIB_Deposit da = null;
        protected DA_CIB_Deposit DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_CIB_Deposit();
                }
                return da;
            }
        }

        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }

        public string[] GetCriteriaArray()
        {
            string[] flds = DA.GetDistinctField();
            System.Collections.ArrayList newFld = new System.Collections.ArrayList();
            foreach (string str in flds)
            {
                newFld.Add(str);
            }
            newFld.Add("YYYYMM1");
            newFld.Add("YYYYMM2");
            return (string[])newFld.ToArray(typeof(string));
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            int option = 0;
            option = Convert.ToInt16(p_criteria[2].ToString());
            bool rmGroup = false,cur=false;
            DA.ResetCriteria();
            string[] ymList,ymCollect;
            ymList = p_criteria[1] as string[];
            string[] accno=null, tablename=null;
            if (string.IsNullOrEmpty(ymList[1]))
            {
                ymList[1] = ymList[0];
            }
            ymCollect = Utility.GetAllMonths(ymList); //取得連續月份
            List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
            foreach (SelectList SL in criteriaList)
            {
                switch (SL.GetField())
                { 
                    case "RM_GROUP":
                        rmGroup = true;
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());    
                        break;
                    case "ACC_NO":
                        accno = SL.GetListArray();
                        break;
                    case "TABLE_NAME":
                        tablename = SL.GetListArray();
                        break;
                    case "CURR_CODE":
                        cur = true;
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());
                        break;
                }
            }
            DataTable dt = null;
            switch (option)
            { 
                case 1:
                    dt = DA.GetSummaryDataByDeposit(ymList, ymCollect, accno,tablename);
                    break;
                case 2:
                    dt = DA.GetSummaryDataByRM(ymList, ymCollect, accno, tablename);
                    //dt = DA.GetSummaryDataByDeposit(ymList, ymCollect, accno);
                    break;
                default:
                    dt = DA.GetSummaryData(ymList, tablename, accno, rmGroup, cur);
                    break;
            }
            return dt;
        }

        public DataTable GetDictionaryTable()
        {
            return DA.GetDictionaryTable();
        }

        public string[] GetCalFields()
        {
            return DA.GetCalFields();
        }
    }
}
